#include "AbstractItem.h"
#include "Defines.h"



// todo: all data type names to common defines.
namespace
{
QString NAME_FIELD_NAME = "name";
QString DESC_FIELD_NAME = "description";
}



AbstractItem::AbstractItem(const int &category, QObject *parent)
    : QObject(parent)
{
    d = new Data();
    d->category = category;
}



void AbstractItem::setData(const int &data_type, const QVariant &data)
{
    switch (data_type)
    {
    case NameData:
        d->name = data;
        break;

    case DescData:
        d->desc = data;
        break;

    case CategoryData:
        d->category = data;
        break;

    default:
        return;
    }

    emit sigRefresh();
}



QVariant AbstractItem::data(const int &data_type) const
{
    switch (data_type)
    {
    case NameData: return d->name;
    case DescData: return d->desc;
    case CategoryData: return d->category;
    }

    return QVariant();
}



int AbstractItem::fieldCount() const
{
    return (DataType_LAST-DataType_FIRST+1);
}



int AbstractItem::itemCount() const
{
    return items().count();
}



QList<AbstractItem*> AbstractItem::items() const
{
    QList<AbstractItem*> ret;
    foreach (AbstractItem* item, findChildren<AbstractItem*>())
    {
        if (!item || item->parent() != this) continue;

        ret.append(item);
    }

    return ret;
}



AbstractItem* AbstractItem::itemAt(const int& index) const
{
    QList<AbstractItem*> list = items();

    if (list.isEmpty() ||
        index < 0 ||
        index >= list.count()) return 0;

    return list.at(index);
}



QString AbstractItem::fieldName(const int& data_type) const
{
    switch (data_type)
    {
    case NameData: return NAME_FIELD_NAME;
    case DescData: return DESC_FIELD_NAME;
    default: return QString::null;
    }
}



int AbstractItem::field(const QString& field_name) const
{
    if (field_name == NAME_FIELD_NAME) return NameData;
    if (field_name == DESC_FIELD_NAME) return DescData;

    return UndefinedData;
}



int AbstractItem::category() const
{
    QVariant v = data(CategoryData);
    if (!v.isValid()) return UNDEFINED;

    bool ok = false;
    int ret = v.toInt(&ok);

    if (!ok) return UNDEFINED;

    return ret;
}



QString AbstractItem::name() const
{
    QVariant v = data(NameData);
    if (!v.isValid()) return QString::null;

    return v.toString();
}



QString AbstractItem::desc() const
{
    QVariant v = data(DescData);
    if (!v.isValid()) return QString::null;

    return v.toString();
}


